// pages/status/status.js
import user_thing from '../../module/use_thing'
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    none: false,
    first: true,
    type: '',
    page: 1,
    on_sale: true,
    saled: false,
    off_sale: false,
    off_saleArr: [],
    on_saleArr: [],
    saledArr: [],
    thisPage: 0
  },
  /**
   * 前往详情页
   */
  goDetails(e) {
    let that = this
    wx.navigateTo({
      url: '../details/details?id=' + that.data[that.data.type + 'Arr'][e.currentTarget.dataset.num].thing_id
    })
  },
  /**
   * 已出售
   */
  staus(e) {
    let that = this
    console.log(that.data[that.data.type + 'Arr'][e.currentTarget.dataset.num])

    wx.showModal({
      title: '已出售？',
      content: '',
      confirmText: '确认',
      success(res) {
        if (res.confirm) {
          let thing_id = that.data[that.data.type + 'Arr'][e.currentTarget.dataset.num].thing_id
          user_thing
            .setThingStatus(thing_id)
            .then(res => {
              console.log(res)
              //
              wx.showToast({
                title: '物品已出售！',
                icon: 'success',
                duration: 1000
              })
              that.getThing()
            })
            .catch(err => {
              console.error(err)

              wx.showToast({
                title: '失败',
                image: '../../image/dele.png',
                duration: 1000
              })
            })
        } else if (res.cancel) {
          // console.log('用户点击取消')
        }
      }
    })
  },
  /**
   *  删除
   */
  deleEnd(e) {
    let that = this
    wx.showModal({
      title: '确认删除此物品？',
      content: '',
      confirmText: '确认删除',
      success(res) {
        if (res.confirm) {
          // console.log('用户点击确定')

          let thing_id = that.data[that.data.type + 'Arr'][e.currentTarget.dataset.num].thing_id
          // console.log(thing_id)
          user_thing
            .deleMyThing(thing_id)
            .then(res => {
              that.getThing()
            })
            .catch(err => {
              wx.showToast({
                title: '删除失败',
                image: '../../image/dele.png',
                duration: 1500
              })
            })
        } else if (res.cancel) {
          // console.log('用户点击取消')
        }
      }
    })
  },
  /**
   * 编辑物品
   */
  edit(e) {
    let that = this

    wx.navigateTo({
      url: '../publish/publish?data=' + JSON.stringify(that.data[that.data.type + 'Arr'][e.currentTarget.dataset.num])
    })
  },

  /**
   * 获取物品
   */
  getThing() {
    let that = this
    user_thing
      .my_thing({
        type: that.data.type, // on_sale(出售中) saled(已出售) off_sale(下架) expired(过期)
        login_token: app.globalData.login_token,
        page: that.data.page
      })
      .then(res => {
        // console.log(res)

        let arr = res.data.list,
          arr_name = that.data.type + 'Arr'
        arr = arr.map(item => {
          // 时间
          let old_time = new Date(item.updated_at || item.created_at).getTime(), //
            now_time = new Date().getTime()
          item.day = 30 - Math.floor((now_time - old_time) / (24 * 3600 * 1000))
          return item
        })
        // console.log(arr)

        that.setData({
          [arr_name]: arr,
          first: false,
          none: false
        })
      })
      .catch(err => {
        // console.log('...')
        that.setData({
          [that.data.type + 'Arr']:[],
          none: true
        })
      })
  },
  /**
   * 改变显示状态
   */
  headerChange(e) {
    this.setData(
      {
        thisPage: e.target.dataset.num,
        type: this.type(e.target.dataset.num)
      },
      () => {
        this.getThing()
      }
    )
  },
  pageChange(e) {
    // console.log(e.detail.current)
    this.setData(
      {
        thisPage: e.detail.current,
        type: this.type(e.detail.current),
        none: false
      },
      () => {
        this.getThing()
      }
    )
  },
  type(num) {
    // console.log(num)

    switch (num * 1) {
      case 0:
        return 'on_sale'
        break
      case 1:
        return 'saled'
        break
      case 2:
        return 'off_sale'
        break
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData(
      {
        thisPage: options.page,
        type: this.type(options.page)
      },
      () => {
        this.getThing()
      }
    )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (!this.data.first) {
      this.setData(
        {
          page: 1
        },
        this.getThing
      )
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    console.log('触底')
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
})
